import {addCart, deleteCart, shoppingCar} from "../../api/api";





export default {
    namespaced:true,
    state:{
        //购物车数组
        cartList:[],
        //选中的物品存放的数组
        checkList:[],

    },
    mutations:{
        changeCartList(state,cartList){
            state.cartList=cartList
        },
        changCheckList(state,checkList){
          state.checkList=checkList
        },

    },
    actions:{
        initCartList(context){
            shoppingCar().then(res => {
                context.commit("changeCartList",res.shoppingCartList)
            })

        },
        addCartList: function (context, courseId) {
            addCart(courseId).then(res => {
                console.log(res)



                //添加购物车东西的时候弹出提示框
                this.$notification.open({
                    message: '通知',
                    description: res.msg,
            })
                context.dispatch("initCartList")
            })
        },
        deleteCartoo:function(context,shoppingCartId){
            deleteCart(shoppingCartId).then(res=>{
                console.log(res)
            })
        }

    },
    getters:{
        number(state){
            return state.cartList.length
        },
        checkAll(state){
            return state.checkList.length!=0&&state.checkList.length==state.cartList.length
        },
        indeterminate(state){
            return state.checkList.length!=0&&state.checkList.length<state.cartList.length
        }


    },

}